
/*This program capture words from the file and displays them if repeated consecutively.
  This program also shows in which line of the test file the repeated words are found.*/

package assignment1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


// TODO: 
/**
 * The Class FileReadAndCompare.
 */
public class FileReadAndCompare 
{

	/**
	 * Read file as string.
	 *
	 * @param filePath the file path
	 * @throws FileNotFoundException the file not found exception
	 */
	public void readFileAsString(String filePath) throws FileNotFoundException
	{
		File file = new File(filePath);
		Scanner fileContent = new Scanner(file);
		int lineNumber = 1;
		while(fileContent.hasNext())
		{
			String currentline = fileContent.nextLine() + "\n";
			compareStringsFromFile(currentline, lineNumber);
			lineNumber = lineNumber + 1;
		}
	}
	
	/**
	 * Compare strings from file once a delimiter is returned true.
	 * @param Input gets the string read from test file.
	 */
	public void compareStringsFromFile(String input, int lineNumber)
	
	{
		String[] inputs = new String[1000]; // {Assumption: string array can compare upto 1000 strings}
		int wordCount = 0;
		String currentWord="";//Reads word at a time
		for(int i=0; i < input.length(); i++)
		{
			char currentCharacter = input.charAt(i);
			if(isDelimeter(currentCharacter)){
				inputs[wordCount] = currentWord;//
				currentWord = "";
				wordCount = wordCount + 1;
		}
			
		else
		{
				currentWord = currentWord + currentCharacter;//cur = this\n
		}
	}
		
	for(int i=1;i<wordCount;i++)
		{
			if(inputs[i].equals(inputs[i-1]))
				System.out.println("Found Common strings: Line # " + lineNumber + " :: " + inputs[i] + "=" + inputs[i-1]);
		}
	}

	/** The delimeter list. */
	static char[] delimeterList= {'\0', '\n', '	', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\'};
	   	 
	/**
	 * This function checks if the character is a delimiter.
	 * @param currentCharacter the current character match against the list of delimiter.
	 * @return true, if the current character is delimiter.
	 * @return false, if the current character is not delimiter.
	 */
	public boolean isDelimeter(char currentCharacter)
	{
		for(int i=0; i < delimeterList.length; i++)
			{
			if(delimeterList[i] == currentCharacter)
				return true;
			}
				return false;
	}
	
	/**
	 * The main method.It reads the file to run by the program.
	 * @param args catches the file path.
	 * @throws FileNotFoundException the file not found exception
	 */
	
	
	public static void main(String[] args) throws FileNotFoundException 
	{
	
		//args[0] has the name and path of test file.
		if(args.length <= 0)
			System.out.println("No file input found. Try java className fileName");
		else
		{
			FileReadAndCompare fileReadAndCompare = new FileReadAndCompare();
			fileReadAndCompare.readFileAsString(args[0]);
		}
	}



}

